<?php

namespace app\admin\controller\information;

use app\common\controller\Backend;
use think\Db;
/**
 * 项目大类
 *
 * @icon fa fa-circle-o
 */
class U8fitem extends Backend
{
    
    protected $model = null;
    //protected $modelClass = null;
    //protected $modelClassDetail = null;
    
    protected $searchFields = "cItem_class,cName";
    
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\information\U8fitem;
        //$this->modelClass = new \app\admin\model\information\U8fitemclass;
        //$this->modelClassDetail = new \app\admin\model\information\U8fitemclassdetail;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
    }
    
    public function index()
    {
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            $this->searchFields = 'citem_class,citem_name';
            return $this->selectpage();
        }
        
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        
        $list = $this->model
        ->where($where)
        ->field('cUser_Id,cUser_Name,cDept')
        ->order($sort, $order)
        ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }
    
    public function getFitemclass(){
        try{
            $requestArr = $this->request->request();
            $classcode = $requestArr['custom']['classcode'];
            if($classcode){
                return $this->selectpage2("fitemss{$classcode}class",['cItemCname','cItemCcode']);
            }
        }catch (\Exception $e){
            return json(['list'=>[]]);
        }
    }
    
    public function getFitemclassdetail(){
        try{
            $requestArr = $this->request->request();
            $classcode = $requestArr['custom']['classcode'];
            $citemccode = $requestArr['custom']['citemccode'];
            
            if($classcode){
                return $this->selectpage2("fitemss{$classcode}",['citemcode','citemname'],['citemccode'=>$citemccode]);
            }
        }catch (\Exception $e){
            return json(['list'=>[]]);
        }
    }
    
    /**
     * 下拉搜索 自定义
     */
    public function selectpage2($tname,$fieldArr,$whereArr = [])
    {
        $condition = [];
        $this->request->filter(['strip_tags']);
        
        $params =$this->request->param();
        
        $condition = $whereArr;
        
        //分页查询
        $page = $this->request->param('pageNumber');
        $limit = $this->request->param('pageSize');
        $limit = $limit ? $limit : 10;
        if (isset($params['cItemCname'])) {
            $condition[implode('|', $fieldArr)] = ['like',"%{$params['cItemCname']}%"];
        }
        if (isset($params['citemname'])) {
            $condition[implode('|', $fieldArr)] = ['like',"%{$params['citemname']}%"];
        }
        
        $total = $this->model->table($tname)->where($condition)
        //->order("id", "DESC")
        ->count();
        $list = $this->model->table($tname)
        ->where($condition)
        ->field(implode(',', $fieldArr))
        //->order("id", "DESC")
        ->limit($limit)
        ->page($page)
        ->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }
    
    //选择项目
    public function chooseproject(){
        $params = $this->request->param();
        
        if (false === $this->request->isAjax()) {
            $this->assign('cItem',$params['cItem']);
            return $this->view->fetch();
        }
        
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        
        try{
            $list = Db::connect('database.sec')->table('fitemss'.$params['cItem'])
            ->where($where)
            ->field('citemcode,citemname,citemccode')
            //->order($sort, $order)
            ->paginate($limit);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }
    
    /* 
     * 
     *  */
     public function getItemData(){
        $params = $this->request->param();
        try {
            $data = Db::connect('database.sec')->table("fitemss{$params['cItem']}class")
            ->field('cItemCname,cItemCcode')
            ->select();
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        
        foreach ($data as $k => $v) {
            $jsList[] = array(
                'id' => $v['cItemCcode'],
                'parent' => '#',
                'text' => __($v['cItemCname']),
            );
        }
        $this->success('','',$jsList);
    } 
}
